#ifndef PARSER_H
#define PARSER_H

#include <iostream>
#include <QString>
#include <QList>
#include <QStack>
#include <QFile>
#include <QFileInfo>
#include <QDir>
#include <QTextStream>
#include <QRegExp>
#include <QMap>

#include <QtGui/QTreeWidget>
#include <QtGui/QTableWidget>

namespace imake {

    class Node {
    public:
        Node(QString name);
        ~Node();
        // Target name
        QString name;
        // List of dependencies
        QList<Node*> deps;
    };

    class MakeInfo {
    public:
        QList<Node*> rootTargets;
        QMap<QString, QString> vars;
        QMap<QString, QString> specialTargets;
    };

    bool isSpecialTarget(QString name);

    // Look for target with specified name. Search starts with
    // call findTarget(<name>, NULL, <path>)
    // Path specifies path to target from tree root.
    Node* findTarget(QString name, Node* node, QStack<QString> &path,
                     QList<imake::Node*> &rootTargets);

    // Add dependency in tree, returns true on success,
    // false means that dependency already exists.
    void addDeps(QString target, QList<QString> deps, QList<Node *> &rootTargets);

    // Open, read and return file as one huge string.
    QString readMakefile (QString fileName);

    // Print specified tree
    void printTree(Node* node, int indent);
    // Print all trees in rootTargets
    void printAll(QList<Node *> &rootTargets);

    // Print specified tree
    void printTree_1(Node* node, QTreeWidgetItem * parent);
    // Print all trees in rootTargets
    void printAll_1(QList<Node *> &rootTargets, QTreeWidget * parent);

	// Print variables
    void printVar(QMap<QString, QString> &vars, QTableWidget * tableWidget);
	
    // Create tree from specified makefile
    void createTree(QString makefile, MakeInfo &mi);

    // It's obvious.
    void clearRootTargets(QList<Node *> &rootTargets);

    // Check is there variables in string (like ${CC}) and
    // replace it by actual values.
    void findAndSetVariables(QString &str, MakeInfo &mi);

    QString unfoldPercentWildcard(QString str);

}

#endif // PARSER_H
